<?php

class _Input {
    const TYPE_NOCLEAN=0; // 不做处理
    const TYPE_INT=1; // 转换成integer
    const TYPE_UINT=2; // 转换成无符号integer
    const TYPE_NUM=3; // 转换成number
    const TYPE_UNUM=4; // 转换成无符号 number
    const TYPE_STR=5; // 转换成string，并去除两边的空格
    const TYPE_NOTRIM=6; // 转换成string，保留空格
    const TYPE_ARRAY=7; // 转换成array
    const TYPE_FILE=8; // 转换成file

    static $data_from = array('g' => '_GET', 'p' => '_POST', 'r' => '_REQUEST', 'c' => '_COOKIE', 's' => '_SERVER', 'e' => '_ENV', 'f' => '_FILES');

    static function clean($source, $varname, $vartype = self::TYPE_NOCLEAN) {
        $sg = &$GLOBALS[self::$data_from($source)];

        $var = isset($sg["$varname"]) ? $sg["$varname"] : "";

        return self::__do_clean($var, $vartype);
    }

    static function __do_clean(&$data, $type) {
        switch ($type) {
            case self::TYPE_INT: {
                    $data = intval($data);
                    break;
                }
            case self::TYPE_UINT: {
                    $data = ($data = intval($data)) < 0 ? 0 : $data;
                    break;
                }
            case self::TYPE_NUM: {
                    $data += 0;
                    break;
                }
            case self::TYPE_UNUM: {
                    $data = ($data += 0) < 0 ? 0 : $data;
                    break;
                }
            case self::TYPE_STR: {
                    $data = trim(strval($data));
                    break;
                }
            case self::TYPE_NOTRIM: {
                    $data = strval($data);
                    break;
                }
            case self::TYPE_ARRAY: {
                    $data = (is_array($data)) ? $data : array();
                    break;
                }
            case self::TYPE_FILE: {
                    if (!is_array($data)) {
                        $data = array(
                            'name' => '',
                            'type' => '',
                            'size' => 0,
                            'tmp_name' => '',
                            'error' => "UPLOAD_ERR_NO_FILE",
                        );
                    }
                    break;
                }
        }
        return $data;
    }

}

?>